package com.powervision.sharesdkdemo.utils

import android.graphics.Bitmap
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.Target
import com.powervision.sharesdkdemo.app.MyApp
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage
import com.tencent.mm.opensdk.modelmsg.WXMiniProgramObject
import com.tencent.mm.opensdk.openapi.IWXAPI

/**
 * @auth: njb
 * @date: 2021/10/20 13:59
 * @desc: 描述
 */
class WxShareUtils private constructor() {
    companion object {
        var req = WXLaunchMiniProgram.Req()
        var miniProgram = WXMiniProgramObject()
        var api: IWXAPI? = null

        private val instance by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            WxShareUtils()
        }

        /**
         * 启动小程序
         */
        @JvmStatic
        fun startMiniProject(path: String) {
            req.userName = AppConstant.SHOP_WX_MINI_PROGRAM_ID // 填小程序原始id
            req.path = path // 拉起小程序页面的可带参路径，不填默认拉起小程序首页
            req.miniprogramType = WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE // 可选打开 开发版，体验版和正式版
            ShareUtils.api?.sendReq(req)
        }

        /**
         * 分享到小程序
         */
        @JvmStatic
        fun shareToWxMiniProject(imgUrl: String, title: String, desc: String, path: String) {
            miniProgram.webpageUrl = "https://www.qq.com"
            miniProgram.miniprogramType = com.tencent.mm.opensdk.modelmsg.WXMiniProgramObject.MINIPTOGRAM_TYPE_RELEASE
            miniProgram.userName = AppConstant.SHOP_WX_MINI_PROGRAM_ID
            miniProgram.path = path
            val wxMediaMessage = WXMediaMessage(miniProgram)
            wxMediaMessage.title = title // 小程序消息title
            wxMediaMessage.description = desc // 小程序消息desc


            Thread(Runnable {
                val bmp: Bitmap?
                try {
                    bmp = Glide.with(MyApp.getInstance())
                            .asBitmap()
                            .load(imgUrl)
                            .centerCrop()
                            .into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                            .get()
                    var bitmap = BitMapUtils.drawWXMiniBitmap(bmp, bmp.height * 5 / 4, bmp.height)
                    if (BitMapUtils.isOverSize(bitmap, 128)) { //是否需要压缩，不需要的话设置bitmap宽高
                        bitmap = BitMapUtils.setBitmapSize(bitmap, 300, 260)
                    }
                    //bitmap转成字节流
                    wxMediaMessage.thumbData = BitMapUtils.bmpToByteArray(bitmap, true)
                } catch (e: Exception) {
                    e.printStackTrace()
                }
                val req = SendMessageToWX.Req()
                req.transaction = buildTransaction("miniProgram")
                req.message = wxMediaMessage
                req.scene = SendMessageToWX.Req.WXSceneSession // 目前只支持会话
                ShareUtils.api?.sendReq(req)
            }).start()
        }


        private fun buildTransaction(type: String?): String {
            return if (type == null) System.currentTimeMillis().toString() else type + System.currentTimeMillis()
        }
    }
}